package com.igeek.boot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.igeek.boot.entity.ContractApprovals;
import com.igeek.boot.entity.Contracts;
import com.igeek.boot.mapper.ContractsMapper;
import com.igeek.boot.service.IContractsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.igeek.boot.utils.QiniuUtils;
import com.igeek.boot.vo.PageQueryVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jwj
 * @since 2023-12-26
 */

@Slf4j
@Service
public class ContractsServiceImpl extends ServiceImpl<ContractsMapper, Contracts> implements IContractsService {

    @Autowired
    private ContractsMapper contractsMapper;

    @Override
    public Page<Contracts> findPage(PageQueryVO pageQueryVO) {
        //获取分页条件
        Integer currentPage = pageQueryVO.getCurrentPage();
        Integer pageSize = pageQueryVO.getPageSize();
        Page<Contracts> page = new Page<>(currentPage, pageSize);
        //封装查询条件  查询的信息可能根据仓库名称、仓库类型、仓库的城市进行查询
        LambdaQueryWrapper<Contracts> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        String queryString = pageQueryVO.getQueryString();
        lambdaQueryWrapper.or().like(StringUtils.hasLength(queryString), Contracts::getContractName, queryString);

        //执行查询
        Page<Contracts> contractsPage = baseMapper.selectPage(page, lambdaQueryWrapper);
        log.info("查询合同表的数据{}",contractsPage.getRecords());
        return contractsPage;
    }

    @Override
    public Page<Contracts> findPageByStatus(PageQueryVO pageQueryVO) {
        Integer currentPage = pageQueryVO.getCurrentPage();
        Integer pageSize = pageQueryVO.getPageSize();
        Page<Contracts> page = new Page<>(currentPage, pageSize);
        LambdaQueryWrapper<Contracts> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        String queryString = pageQueryVO.getStatus();
        lambdaQueryWrapper.eq(StringUtils.hasLength(queryString), Contracts::getStatus, queryString);
        Page<Contracts> contractsPage = baseMapper.selectPage(page, lambdaQueryWrapper);
        log.info("查询合同表的数据{}",contractsPage.getRecords());
        return contractsPage;
    }

    @Override
    public String upload(MultipartFile file) throws IOException {
        if(file!=null){
            String oldName = file.getOriginalFilename();
            if(StringUtils.hasLength(oldName)) {
                String newName ="CONTRACT_"+ UUID.randomUUID() + oldName.substring(oldName.lastIndexOf("."));
                log.info("newName{}", newName);
                String upName = QiniuUtils.uploadForQiniu(file.getInputStream(), newName);
                return upName;
            }
        }
        return null;
    }

    @Override
    public boolean addContract(Contracts contracts) {
        contracts.setContractNumber("CONTRACT_"+ UUID.randomUUID());
        contracts.setStatus("待审批");
        int insert = baseMapper.insert(contracts);
        return insert>0;
    }

    @Override
    public boolean updateStatus(Integer id, String status) {
        UpdateWrapper<Contracts> wrapper = new UpdateWrapper<>();
        Contracts contracts = new Contracts();
        wrapper.eq("contract_id",id);
        contracts.setStatus(status);
        int update = baseMapper.update(contracts, wrapper);
        return update>0;
    }

    @Override
    public boolean delContract(Integer id) {
        Contracts contracts = new Contracts();
        contracts.setContractId(id);
        int i = baseMapper.deleteById(contracts);
        return i>0;
    }

    //获取饼图数据
    @Override
    public Map<String, Integer> findPie() {
        //1.获取待审批的合同总数
        Long count1 = baseMapper.selectCount(new LambdaQueryWrapper<Contracts>().eq(Contracts::getStatus, "待审批"));
        Long count2 = baseMapper.selectCount(new LambdaQueryWrapper<Contracts>().eq(Contracts::getStatus, "审批中"));
        Long count3 = baseMapper.selectCount(new LambdaQueryWrapper<Contracts>().eq(Contracts::getStatus, "履约中"));
        Long count4 = baseMapper.selectCount(new LambdaQueryWrapper<Contracts>().eq(Contracts::getStatus, "已完结"));
        HashMap<String, Integer> map = new HashMap<>();
        map.put("count1",count1.intValue());
        map.put("count2",count2.intValue());
        map.put("count3",count3.intValue());
        map.put("count4",count4.intValue());
        return map;
    }
}
